<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Category extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		//$this->output->enable_profiler(1);

		$this->load->model('category_model');
		$this->load->model('product_category_model');

		$this->template->set_breadcrumb('Danh mục', 'backend/category');
	}

	public function index()
	{
		//$this->auth->check_uri_permissions(true, 'backend/login');
		$this->template->title('SiteName', 'Danh mục');
		$this->template->build('backend/category/category', $this->data);
	}

	public function load($category_id = NULL)
	{
		$this->load->helper('text');
		$this->load->helper('string');

		if ($category_id)
		{
			$category = $this->category_model->find(array('id' => $category_id))->row();
			echo json_encode($category);
			return;
		}

		$categories = array();
		$this->category_model->single_path($this->category_model->list_all(), &$categories);

		$data['total_record'] = $this->category_model->count();

		$data['rows'] = "";
		foreach ($categories as $key => $row)
		{
			$data['rows'].= "<tr><td><input type=\"checkbox\" name=\"items[]\" value=\"{$key}\" /></td>";
			$data['rows'].= "	<td>{$row['name']}</td>";
			$data['rows'].= "	<td>".nl2br(word_limiter($row['description'], 30))."</td>";
			$data['rows'].= "	<td><input type=\"text\" class=\"input ui-corner-all position\" name=\"item_positions[{$key}]\" value=\"{$row['position']}\" /></td>";
			$data['rows'].= "	<td align=\"center\"><div><a href=\"#\" class=\"edit btn_edit\" rel=\"{$key}\">Edit</a></div></td>";
			$data['rows'].= "</tr>";
		}
		$data['message'] = 'OK';
			
		echo json_encode($data);
	}

	public function create()
	{
		$response = array(
			'success' => 0,
			'message' => '',
		);

		$validator = $this->form_validation;
		$validator->set_rules('name', 'Tên', 'trim|required|xss_clean|min_length[2]|max_length[255]');
		$validator->set_rules('parent_id', 'Danh mục cha', 'trim|xss_clean');
		$validator->set_rules('description', 'Mô tả', 'trim|xss_clean');

		if($validator->run())
		{
			$data = array(
				'name' => $validator->set_value('name'),
				'parent_id' => $validator->set_value('parent_id'),
				'description' => $validator->set_value('description'),
				'created' => date('Y-m-d H:i:s'),
			);

			if($this->category_model->insert($data))
			{
				$response['success'] = 1;
			} else {
				$response['message'] = "Lỗi.";
			}
		} else {
			$response['message'] = strip_tags(validation_errors());
		}

		echo json_encode($response);
	}

	public function delete($category_id = NULL)
	{
		if($this->input->is_ajax_request())
		{
			if($category_ids = $this->input->post('items'))
			{
				foreach ((array)$category_ids as $category_id)
				{
					$this->category_model->delete($category_id);
					$this->product_category_model->delete(array('category_id' => $category_id));
				}
			}

			$data['message'] = 'OK';
			die(json_encode($data));
		}
	}

	public function update($category_id)
	{
		$response = array(
				'success' => 0,
				'message' => '',
		);

		$validator = $this->form_validation;

		$validator->set_rules('name', 'Tên', 'trim|required|xss_clean|min_length[2]|max_length[255]');
		$validator->set_rules('parent_id', 'Danh mục cha', 'trim|xss_clean|integer');
		$validator->set_rules('description', 'Mô tả', 'trim|xss_clean');

		if($validator->run())
		{
			$data = array(
				'name' => $validator->set_value('name'),
				'parent_id' => $validator->set_value('parent_id'),
				'description' => $validator->set_value('description'),
			);

			if($data['parent_id'] == $category_id)
			{
				$response['message'] = 'Danh mục không thể là con của chính nó!';
			}
			else
			{
				$this->category_model->update(array('id' => $category_id), $data);
					
				$response['success'] = 1;
				$response['message'] = 'Thành công';
			}
		} else {
			$response['message'] = strip_tags(validation_errors());
		}

		echo json_encode($response);
	}

	public function update_position()
	{
		if($this->input->is_ajax_request())
		{
			if($item_positions = $this->input->post('item_positions'))
			{

				foreach ((array)$item_positions as $item => $position)
				{
					$this->category_model->update(array('id' => $item), array('position' => $position));
				}
			}

			$data['message'] = 'OK';
			die(json_encode($data));
		}
	}

	public function gen_options()
	{
		$name = ($this->input->post('name')) ? $this->input->post('name') : 'parent_id';
		echo json_encode($this->category_model->build_dropdown($name, "id=\"{$name}\" class=\"input ui-corner-all\""));
	}
}




